

//carrega pdfs com pdfjs

//
// See README for overview
//


    //
    // Fetch the PDF document from the URL using promices
    //
    //
    // NOTE: 
    // Modifying the URL below to another server will likely *NOT* work. Because of browser
    // security restrictions, we have to use a file server with special headers
    // (CORS) - most servers don't support cross-origin browser requests.
    //
    var url = 'null';

    //
    // Disable workers to avoid yet another cross-origin issue (workers need the URL of
    // the script to be loaded, and currently do not allow cross-origin scripts)
    //
    PDFJS.disableWorker = true;
    var pdfDoc = null,
        pageNum = 1,
	pageNumRemoto=1,
        scale = 0.8,
        canvas = null,
        canvasLoza = null,
	modoBorracha=0,
        ctx=null;       
    //
    // Get page info from document, resize canvas accordingly, and render page
    //
    function renderPagePri(num) {
      if(pdfDoc==null) return;
      // Using promise to fetch the page
      pdfDoc.getPage(num).then(function(page) {
        pagewidth=  page.getViewport(1.0).width;
        var viewport = page.getViewport(canvas.width/pagewidth);        
        canvas.height = viewport.height;        
        canvas.width = viewport.width;         
        canvasLoza.css("width",viewport.width+"px"); 
        canvasLoza.css("height",viewport.height+"px");     
        canvasLoza.attr("width", viewport.width); 
        canvasLoza.attr("height", viewport.height); 
         for(var i=0; i<pdfDoc.numPages;i++){
	    	    $('#divDocPdf',window.parent.canvasFrame.document).append("<canvas id=\"loza"+(i+1)+"\" style=\"position: absolute; top:0; left:0px; z-index:10; visibility: hidden; width:"+canvas.width+"px; height:"+canvas.height+"px\" width=\""+canvas.width+"\" height=\""+canvas.height+"\"></canvas>");
	        }
            $("#the-canvas",window.parent.canvasFrame.document).css("width",canvas.width+"px"); 
            $("#the-canvas",window.parent.canvasFrame.document).css("height",canvas.height+"px"); 
 
        // Render PDF page into canvas context
        var renderContext = {
          canvasContext: ctx,
          viewport: viewport
        };
        page.render(renderContext);
      });

    }

    function renderPage(num) {
      if(pdfDoc==null) return;
      // Using promise to fetch the page
      pdfDoc.getPage(num).then(function(page) {
        pagewidth=  page.getViewport(1.0).width;
        var viewport = page.getViewport(canvas.width/pagewidth);        
        canvas.height = viewport.height;        
        canvas.width = viewport.width;         
        canvasLoza.css("width",viewport.width+"px"); 
        canvasLoza.css("height",viewport.height+"px");       
        // Render PDF page into canvas context
        var renderContext = {
          canvasContext: ctx,
          viewport: viewport
        };
        page.render(renderContext);
      });
    }

    //
    // Go to previous page
    //
    function goPrevious() {
      if (pageNum <= 1)
        return;
      pageNum--;
      renderPage(pageNum);
    }

    //
    // Go to next page
    //
    function goNext() {
      if (pageNum >= pdfDoc.numPages)
        return;
      pageNum++;
      renderPage(pageNum);
    }

    //
    // Asynchronously download PDF as an ArrayBuffer
    //
    function openPDF(urlInter){
        url=urlInter;
        PDFJS.getDocument(url).then(function getPdfHelloWorld(_pdfDoc) {
            pdfDoc = _pdfDoc;
            $("#totalPagina").html(""+pdfDoc.numPages);
	    $("#nPagina").html(""+1);
            renderPagePri(pageNum);
        });        
    }       
//fim





function limparLoza(){
    var elem = document.getElementById('loza');
    var ctx = elem.getContext('2d');                
    ctx.clearRect(0, 0, elem.width, elem.height);
}
//fim




function desenharMouse(x,y,x2,y2,id){
    var elem = window.parent.canvasFrame.document.getElementById('loza'+id);
    var ctx = elem.getContext('2d');
    if(modoBorracha==0){
	ctx.fillStyle   = '#264761';
	ctx.beginPath();
	    ctx.moveTo(x,y);
	    ctx.lineTo(x2,y2);
	ctx.stroke();
    }
    else{
     ctx.clearRect(x2-10,y2-10, 20,20 );  
    }
}

function ajustarFrameCanvas(){
    //ajusta os componentes da pagina de acordo com o zoom
    var largWin=$(window).width();
    var altWin=$(window).height();      
    var widthTempStr=canvasLoza.css("width");
    var heightTempStr=canvasLoza.css("height");
    var widthInt=parseInt(widthTempStr.substr(0,widthTempStr.length-2 ))+20;
    var heightInt=parseInt(heightTempStr.substr(0,heightTempStr.length-2 ))+20;
    if(widthInt< (largWin-20)){
        $("#canvasFrame").attr("width",widthInt);
    } 
    else{
        $("#canvasFrame").attr("width",""+(largWin-20));
    }
    if(heightInt < (altWin-160)){
        $("#canvasFrame").attr("height",""+(heightInt));
    } 
    else{
        $("#canvasFrame").attr("height",""+(altWin-160));
    }

    //fim

}

 
